package logic

import (
	"context"
	"go.uber.org/zap"
	"smart-health-platforms/pkg"
	"user_srv/internal/svc"
	"user_srv/model"

	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectSearchListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectSearchListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectSearchListLogic {
	return &CollectSearchListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 模糊搜索
func (l *CollectSearchListLogic) CollectSearchList(in *user.CollectSearchListRequest) (*user.CollectSearchListResponse, error) {
	var coll model.Collection
	coll.UserId = int32(in.UserId)
	collections, _, err := coll.SearchCollections(in.Name, int(in.Page), int(in.PageSize))
	if err != nil {
		pkg.Error("失败", zap.Error(err))
		return nil, err
	}
	var collList []*user.CollectSearchList
	for _, collection := range collections {
		collList = append(collList, &user.CollectSearchList{
			GoodId: int64(collection.GoodId),
			SkuId:  int64(collection.SkuId),
			Price:  float32(collection.Price),
			Num:    int64(collection.Num),
		})
	}
	return &user.CollectSearchListResponse{
		List: collList,
	}, nil
}
